from flask import Flask
from settings import map_config
from comment.utils.log import create_logger
#负责创建APP对象
def create_app(config_type):
    app=Flask(__name__)
    #加载项目的配置
    app.config.from_object(map_config[config_type])

    #初始化限流器
    from comment.utils.limiter import limiter as lmt
    lmt.init_app(app)

    #加载日志处理的工具
    create_logger(app)

    #初始化SqlAlchemy
    from comment.models import db
    db.init_app(app)

    #初始化redis数据库的连接
    from comment.utils.shopping_redis import redis_client
    redis_client.init_app(app)

    #添加请求钩子
    from comment.utils.requests_wares import jwt_request_authorization
    # 所有请求服务器的操作都会经过这个请求钩子，也就是这个是全局的请求钩子
    app.before_request(jwt_request_authorization)

    #加载模块的蓝图
    from Shopping.resources.user import user_bp
    from Shopping.resources.index import index_bp
    from Shopping.resources.goods import goods_bp
    from Shopping.resources.coupon import coupon_bp
    from Shopping.resources.cart import cart_bp
    app.register_blueprint(user_bp,url_prefix='/user')
    app.register_blueprint(index_bp,url_prefix='/index')
    app.register_blueprint(goods_bp)
    app.register_blueprint(coupon_bp)
    app.register_blueprint(cart_bp)
    return app